//
//  GuessViewController.m
//  Hand shadow
//
//  Created by A-01 on 2020/7/21.
//  Copyright © 2020 A-01. All rights reserved.
//

#import "GuessViewController.h"
#import "DoneViewController.h"

@interface GuessViewController ()

@property (nonatomic, weak)UIImageView * showImageView;
@property (nonatomic, weak)UILabel * scoreLabel;
@property (nonatomic, strong)NSMutableArray * pictureArr;
@property (nonatomic, strong)UIButton * one_button;
@property (nonatomic, strong)UIButton * two_button;
@property (nonatomic, strong)NSArray * leftArr;
@property (nonatomic, strong)NSArray * rightArr;
@property (nonatomic, strong)NSArray * checkArr;
@property (nonatomic, assign)int index;
@property (nonatomic, assign)int score;

@end

@implementation GuessViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:YES];
    
    self.index = 0;
    self.score = 0;
    
    [self.one_button setTitle:self.leftArr[self.index] forState:UIControlStateNormal];
    [self.two_button setTitle:self.rightArr[self.index] forState:UIControlStateNormal];
    self.showImageView.image = [UIImage imageNamed:self.pictureArr[self.index]];
    self.scoreLabel.text = [NSString stringWithFormat:@"Your score: %d",self.score];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.index = 0;
    self.titleLabel.text = @"Guess";
    [self setupUI];
}

- (void)setupUI{
        
    UIImageView * showImageView = [UIImageView new];

    showImageView.image = [UIImage imageNamed:self.pictureArr[self.index]];
    showImageView.backgroundColor = [UIColor colorWithHexString:@"BDE0F9"];
    showImageView.contentMode = UIViewContentModeScaleAspectFit;

    self.showImageView = showImageView;

    [self.view addSubview:self.showImageView];
    
    
    UILabel * scoreLabel = [UILabel new];
    
    scoreLabel.text = [NSString stringWithFormat:@"Your score: %d",self.score];
    scoreLabel.textColor = [UIColor colorWithHexString:@"2C8FB4"];
    scoreLabel.font = [UIFont fontWithName:@"Chalkduster" size:18];
    scoreLabel.textAlignment = NSTextAlignmentRight;
    
    self.scoreLabel = scoreLabel;
    
    [self.view addSubview:self.scoreLabel];
    
    self.one_button = [self setupButtonWithBackImage:@"hand_01" title:self.leftArr[self.index]];
    [self.view addSubview:self.one_button];
    self.two_button = [self setupButtonWithBackImage:@"hand_04" title:self.rightArr[self.index]];
    [self.view addSubview:self.two_button];
}


- (void)clickButton:(UIButton *)button{
    
    NSString * res = button.titleLabel.text;
    
    if ([res isEqualToString:self.checkArr[self.index]]) {
        
        if (self.index != 12) {
            
            self.index ++;
            self.score += 10;
            
            [self.one_button setTitle:self.leftArr[self.index] forState:UIControlStateNormal];
            [self.two_button setTitle:self.rightArr[self.index] forState:UIControlStateNormal];
            self.showImageView.image = [UIImage imageNamed:self.pictureArr[self.index]];
            self.scoreLabel.text = [NSString stringWithFormat:@"Your score: %d",self.score];
        }else{
            
            DoneViewController * doneVC = [DoneViewController new];
            
            doneVC.score = self.score;
            doneVC.type = @"done";
            
            [self.navigationController pushViewController:doneVC animated:YES];
        }
    }else{
        
        DoneViewController * doneVC = [DoneViewController new];
        
        doneVC.score = self.score;
        doneVC.type = @"no";
        
        [self.navigationController pushViewController:doneVC animated:YES];
    }
    
}

- (void)viewDidLayoutSubviews{
        
    [super viewDidLayoutSubviews];

    [self.showImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.view);
        make.leading.mas_equalTo(self.view).offset(80);
        make.width.height.mas_equalTo(180);
    }];
    
    [self.scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(5);
        make.trailing.mas_equalTo(self.view).offset(-60);
    }];
    
    [self.one_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(kScreenW / 2 + 25);
        make.top.mas_equalTo(self.showImageView).offset(15);
    }];
    
    [self.two_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.one_button.mas_trailing).offset(15);
        make.top.mas_equalTo(self.one_button.mas_bottom).offset(45);
    }];
}

- (UIButton *)setupButtonWithBackImage:(NSString *)img title:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
    [button setTitleColor:ZRWhiteColor forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    button.titleEdgeInsets = UIEdgeInsetsMake(17, 0, 0, 0);
    
    return button;;
}


- (NSMutableArray *)pictureArr{
    
    if (!_pictureArr) {
        
        _pictureArr = [NSMutableArray arrayWithCapacity:13];
        for (int i = 1; i < 14; i++) {
            
            NSString * res= [NSString stringWithFormat:@"enjoy_%02d",i];
            [_pictureArr addObject:res];
        }
    }
    
    return _pictureArr;
}

- (NSArray *)leftArr{
    
    if (!_leftArr) {
        
        _leftArr = @[@"rabbit",@"dog",@"bird",@"deer",@"deer",@"bird",@"tiger",@"lion",@"cat",@"koala",@"squirrel",@"dog",@"rabbit"];
    }
    
    return _leftArr;
}


- (NSArray *)rightArr{
    
    if (!_rightArr) {
        
        _rightArr = @[@"mouse",@"cat",@"swan",@"snake",@"kangaroo",@"hawk",@"kalong",@"mouse",@"bear",@"dog",@"bird",@"rabbit",@"cat"];
    }
    
    return _rightArr;
}


- (NSArray *)checkArr{
    
    if (!_checkArr) {
        
        _checkArr = @[@"rabbit",@"dog",@"swan",@"deer",@"deer",@"hawk",@"tiger",@"lion",@"bear",@"dog",@"squirrel",@"rabbit",@"rabbit"];
    }
    
    return _checkArr;
}


@end
